package top.revery.account.service;


import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.response.AlipayTradePagePayResponse;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.revery.api.config.ZhifubaoConfig;
import java.math.BigDecimal;


/**
 * @author denghao
 * @title
 * @package
 * @date 2019/12/9 18:38
 */
@Service
@Slf4j
public class ZhifuBaoService {
   // @GlobalTransactional
    public String pay(BigDecimal money){
        AlipayClient alipayClient = new DefaultAlipayClient(ZhifubaoConfig.URL, ZhifubaoConfig.App_id, ZhifubaoConfig.RSA_PRIVATE_KEY, ZhifubaoConfig.FORMAT, ZhifubaoConfig.charset, ZhifubaoConfig.alipay_public_key, ZhifubaoConfig.sign_type);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
        model.setSubject("你的充值");
        model.setOutTradeNo(String.valueOf(System.currentTimeMillis()));
        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        model.setTotalAmount(money.toString());
        model.setBody("你的额度");
        model.setPassbackParams("sss");
        request.setNotifyUrl(ZhifubaoConfig.notify_url);
        request.setReturnUrl(ZhifubaoConfig.return_url);
        request.setBizModel(model);
        String orderStr = "";
        try {
            //这里和普通的接口调用不同，使用的是sdkExecute
           AlipayTradePagePayResponse res = alipayClient.pageExecute(request);
            System.out.println("返回的参数是："+res);
            orderStr = res.getBody();
            System.out.print("==========================");
            System.out.println(orderStr);//就是orderString 可以直接给客户端请求，无需再做处理。
        } catch (AlipayApiException e) {
            e.printStackTrace();
            log.info("与支付宝交互出错，未能生成订单，请检查代码！");
        }
        return orderStr;
    }

}
